package com.xxl.job.executor.controller;

import com.xxl.job.core.log.XxlJobLogger;
import com.xxl.job.executor.dto.AppConfig;
import com.xxl.job.executor.dto.ResultDto;
import com.xxl.job.executor.model.BasTradeImport;
import com.xxl.job.executor.model.ZyzTempMpdel;
import com.xxl.job.executor.service.TestService;
import com.xxl.job.executor.service.TradeImportService;
import com.xxl.job.executor.util.ExcelReader;
import com.xxl.job.executor.util.FTPUtil;
import com.xxl.job.executor.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: ZYZ
 * @DateTime: 2020/2/27 15:54
 * @Description:
 */
@RestController
public class IndexController extends AppConfig {

    @Autowired
    private FTPUtil ftpUtil;

    @Autowired
    TradeImportService tradeImportService;

    @Autowired
    TestService testService;

    @Value("${ftp.local.save.basepath}")
    private String localSavePath;

    private final String FILE_MAP_SUCCESS = "result";
    private final String FILE_DIR = "sycm";
    private final String EXCEL_KF = "kf";
    private final String EXCEL_DC = "dc";


    public Object index() {

        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            //先下载ftp上的所有文件
            Map<String, Object> fileMap = ftpUtil.downLoadTableFile(FILE_DIR, localSavePath, EXCEL_KF);
            //遍历本地文件进行处理
            List<String> fileNameList = FileUtil.getFileNames(localSavePath + "/" + FILE_DIR);

            if (null != fileNameList && fileNameList.size() > 0) {
                for (String fileName : fileNameList) {
                    String[] strArr = fileName.split("_");
                    if (strArr.length > 1) {
                        if (EXCEL_KF.equals(strArr[1])) {
                            String shopId = strArr[0];
                            if (tradeImportService.checkShopId(shopId)) {
                                String fileLocalPath = localSavePath + "/" + FILE_DIR + "/" + fileName;

                                //开始执行文件解析和导入
                                List<BasTradeImport> list = ExcelReader.readExcel(fileLocalPath, 5, BasTradeImport.class);

                                if (null != list && list.size() > 0) {

                                    System.out.println(list.get(0).getMakeDate());
                                    Date queryDate = df.parse(list.get(0).getMakeDate());
                                    int count = tradeImportService.queryTradeRepition(shopId, queryDate);
                                    if (count == 0) {
                                        ResultDto result = tradeImportService.insertTradeImportNew(list, shopId);
                                    }
                                }
                                //执行服务器文件备份
                                ftpUtil.renameFile(FILE_DIR, fileName);
                                System.out.println(shopId + " ****** " + fileName);
                                //删除本地文件
                                FileUtil.removeFile(fileLocalPath);
                                //延迟执行下一个文件
                                Thread.sleep(10 * 1000);
                            } else {
                                System.out.println(shopId + " 无效：" + fileName);
                            }

                        }
                    }

                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        return null;
    }


    @GetMapping("test")
    public Object test() {


        List<ZyzTempMpdel> list = testService.list();
        for (int j = 0; j < list.size(); j++) {
            System.out.println(j % 2);
            ;
            ZyzTempMpdel model = list.get(j);
            model.setCol3("1111");
            testService.updateModel(model);
            XxlJobLogger.log(model.toString());
        }


        return "";

    }
}
